package tech.aistar.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.dto.Parper;
import tech.aistar.entity.*;
import tech.aistar.service.*;
import tech.aistar.util.CompilerUtil;

import javax.servlet.http.HttpSession;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 本类包含：
 * 1.
 */

@Controller
@RequestMapping("/parper")
public class ParperAction {
    //试卷库
    @Autowired
    private IParperBankService parperBankService;
    //试卷中间库
    @Autowired
    private IQuestionpaperService questionpaperService;
    //试卷时间表
    @Autowired
    private IExaInfoService exaInfoService;
    //单选
    @Autowired
    public ISinchoiceService sinchoiceService;
    //多选
    @Autowired
    private IManchoiceService manchoiceService;
    //填空
    @Autowired
    private ICompletionService completionService;
    //编程
    @Autowired
    public IProgrammingService programmingService;
    //收藏
    @Autowired
    public ICollectService collectService;

    //在试卷详情页展示题目数量
    @RequestMapping("/start/{parperId}")
    public String count(Model model,@PathVariable("parperId") Long parperId){
        ParperBank parperBank = parperBankService.findparper(String.valueOf(parperId));
        String parperName = parperBank.getParperName();//试卷名称
        String parperDiff = parperBank.getParperDiff();//试卷难度
        String parperLanguage = parperBank.getParperLanguage();//试卷类别
        Long parperCount = parperBank.getParperCount();//使用次数
        Long sin = questionpaperService.countType(parperId, 1L);//单选题的个数
        Long man = questionpaperService.countType(parperId, 2L);//多选题的个数
        Long cmo = questionpaperService.countType(parperId, 3L);//填空
        Long pro = questionpaperService.countType(parperId, 4L);//编程




        List list = new ArrayList();
        list.add(parperId);
        list.add(parperName);
        list.add(parperLanguage);
        list.add(parperDiff);
        list.add(parperCount);
        list.add(sin);
        list.add(man);
        list.add(cmo);
        list.add(pro);

        model.addAttribute("paperList",JSON.toJSON(list));
        return  "start";
    }

    //每一次开始插入试卷时间
    @RequestMapping("/exaInfo")
    @ResponseBody
    public String exaInfo(Model model, String parperId, String score,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        Long usId = user.getUsId();//用户id
        System.out.println("usIdusIdusIdusIdusIdusId"+usId);
        System.out.println(parperId);
        System.out.println(score);
        String data = "获取考试编号失败";
        if (usId != null){
            ExaInfo exaInfo = new ExaInfo();
            Calendar calendar = Calendar.getInstance();//获取现在时间
            Date time=calendar.getTime();//获取毫秒数
            exaInfo.setTestStart(time);//开始时间
            exaInfo.setTestDate(time);//开始日期
            calendar.add(Calendar.MINUTE,30);//30分钟后的时间
            Date time1 = calendar.getTime();
            exaInfo.setTestEnd(time1);//考试结束时间
            exaInfo.setPaperId(Long.valueOf(parperId));//试卷号
            exaInfo.setTestValue(score);//总分
            exaInfo.setUsId(usId);
            int i = exaInfoService.insertExaInfo(exaInfo);
            if (i != 0){
                //根据开始时间获取考试号
                Long testId = exaInfoService.findByStartTime(usId);
                System.out.println(testId);
                data = String.valueOf(testId);
            }
        }
        else{
            data = "请先登录";
        }
        return data;
    }


    /**
     * 下面的几个方法用来查询试卷中的题目
     */

    //定义试卷类来存放要传到前端的试卷
    Parper parper = new Parper();
    //单选题
    @RequestMapping("/choose/{parperId}/{testId}")
    public String Sinchoice(Model model, @PathVariable("parperId") Long parperId,@PathVariable("testId") Long testId){
        ExaInfo findexa = exaInfoService.findexa(String.valueOf(testId));//根据考试号来查所有
        Date testEnd = findexa.getTestEnd();//获取考试结束时间

        List<Sinchoice> byPaperId = sinchoiceService.findByPaperId(parperId);//通过试卷号查找
        parper.setParperId(Long.valueOf(parperId));//试卷号
        parper.setSinchoiceList(byPaperId);//将单选题放入试卷对象
        parper.setTestEnd(testEnd);//考试结束时间
        parper.setTestId(testId);//考试号

        model.addAttribute("parper", JSON.toJSON(parper));
        return "choose";
    }

    //多选题
    @RequestMapping("/manchoose/{parperId}/{testId}")
    public String Manchoice(Model model , @PathVariable("parperId") String parperId,@PathVariable("testId") Long testId){
        ExaInfo findexa = exaInfoService.findexa(String.valueOf(testId));//根据考试号来查所有
        Date testEnd = findexa.getTestEnd();//获取考试结束时间

        List<Manchoice> byPaperId = manchoiceService.findByPaperId(Long.valueOf(parperId));
        parper.setParperId(Long.valueOf(parperId));//试卷号
        parper.setManchoiceList(byPaperId);//将多选题放入试卷对象
        parper.setTestEnd(testEnd);//考试结束时间
        parper.setTestId(testId);//考试号

        model.addAttribute("parper", JSON.toJSON(parper) );
        return "manchoose";
    }

    //填空题
    @RequestMapping("/fillTheBlank/{parperId}/{testId}")
    public String Completion(Model model,@PathVariable("parperId") String parperId ,@PathVariable("testId") Long testId){
        ExaInfo findexa = exaInfoService.findexa(String.valueOf(testId));//根据考试号来查所有
        Date testEnd = findexa.getTestEnd();//获取考试结束时间

        List<Completion> byPaperId = completionService.findByPaperId(Long.valueOf(parperId));
        parper.setParperId(Long.valueOf(parperId));//试卷号
        parper.setCompletionList(byPaperId);//将填空题放入对象
        parper.setTestEnd(testEnd);//考试结束时间
        parper.setTestId(testId);//考试号

        model.addAttribute("parper", JSON.toJSON(parper) );
        return "fillTheBlank";
    }

    //编程题
    @RequestMapping("/programe/{parperId}/{testId}")
    public String Programming(Model model ,@PathVariable("parperId") String parperId ,@PathVariable("testId") Long testId){
        ExaInfo findexa = exaInfoService.findexa(String.valueOf(testId));//根据考试号来查所有
        Date testEnd = findexa.getTestEnd();//获取考试结束时间
        List<Programming> byPaperId = programmingService.findByPaperId(1L);
        parper.setParperId(Long.valueOf(parperId));//试卷号
        parper.setProgrammingList(byPaperId);//将编程题放入对象
        parper.setTestEnd(testEnd);//考试结束时间
        parper.setTestId(testId);//考试号

        model.addAttribute("parper",JSON.toJSON(parper));
        return "programe";
    }

    //编程题工具方法
    @RequestMapping("/comp")
    public @ResponseBody JSONObject run(@RequestParam String code, @RequestParam String lang){
//        System.out.println(code);
//        System.out.println(lang);
//
        CompilerUtil compilerUtil = new CompilerUtil();
//        System.out.println(compilerUtil.complier(code, lang));

        return  JSONObject.parseObject(compilerUtil.complier(code,lang));
    }

    //收藏功能
    @RequestMapping("/addCollect")
    @ResponseBody
    public Integer collect(Model model, Long queId, Integer  queType,HttpSession session){
        User user = (User) session.getAttribute("user");
        Long usId = user.getUsId();//用户id

        String colType = ""; //题目类型
        String languageType = "";//语言种类
        String colDiff = "";//难度
        String colStem = "";//题目内容
        if (queType == 1){
            Sinchoice sinchoice = sinchoiceService.findById(queId);
            String sinLanguage = sinchoice.getSinLanguage();
            String sinDiff = sinchoice.getSinDiff();
            String sinStem = sinchoice.getSinStem();
            colType = "单选题";
            languageType = sinLanguage;
            colDiff = sinDiff;
            colStem = sinStem;
        }
        else if (queType == 2){
            Manchoice manchoice = manchoiceService.findById(queId);
            String manLanguage = manchoice.getManLanguage();
            String manDiff = manchoice.getManDiff();
            String manStem = manchoice.getManStem();
            colType = "多选题";
            languageType = manLanguage;
            colDiff = manDiff;
            colStem = manStem;
        }
        else if (queType == 3){
            Completion completion = completionService.findById(queId);
            String comLanguage = completion.getComLanguage();
            String comDiff = completion.getComDiff();
            String comStem = completion.getComStem();
            colType = "填空题";
            languageType = comLanguage;
            colDiff = comDiff;
            colStem = comStem;
        }
        else if (queType == 4){
            Programming programming = programmingService.findById(queId);
            colType = "编程题";
            languageType = programming.getProLanguage();
            colDiff = programming.getProDiff();
            colStem = programming.getProStem();
        }

        Collect collect = new Collect();
        collect.setLanguageType(languageType);//题目类型
        collect.setColDiff(colDiff);//难度
        collect.setColStem(colStem);//题目内容
        collect.setColType(colType);//题目类型
        collect.setColId(usId);//用户Id
        collect.setQueId(queId);//题目Id

        Integer data = 0;
        if (queType == 1){
            List<Collect> serviceQueId = collectService.findQueId(usId, "单选题", queId);
            if (serviceQueId.size() == 0){
                Integer integer = collectService.InsertAll(collect);
                data = integer;
            }else {
                data = 1;
            }
        }
        else if (queType == 2){
            List<Collect> serviceQueId = collectService.findQueId(usId, "多选题", queId);
            if (serviceQueId.size() == 0){
                Integer integer = collectService.InsertAll(collect);
                data = integer;
            }else {
                data = 1;
            }
        }
        else if (queType == 3){
            List<Collect> serviceQueId = collectService.findQueId(usId, "填空题", queId);
            if (serviceQueId.size() == 0){
                Integer integer = collectService.InsertAll(collect);
                data = integer;
            }else {
                data = 1;
            }
        }
        else if (queType == 4){
            List<Collect> serviceQueId = collectService.findQueId(usId, "编程题", queId);
            if (serviceQueId.size() == 0){
                Integer integer = collectService.InsertAll(collect);
                data = integer;
            }else {
                data = 1;
            }
        }
        return data;
    }

    //删除收藏
    @RequestMapping("/delCollect")
    @ResponseBody
    public Integer collect(Long queId, Integer  queType,HttpSession session){
        User user = (User) session.getAttribute("user");
        Long usId = user.getUsId();//用户id
        Integer data = 0;
        if (queType == 1){
            List<Collect> serviceQueId = collectService.findQueId(usId, "单选题", queId);
            if (serviceQueId.size() == 0){
                data = 1;
            }
            else{
                Integer integer = collectService.delCollect(usId, "单选题", queId);
                data = integer;
            }

        }
        else if (queType == 2){
            List<Collect> serviceQueId = collectService.findQueId(usId, "单选题", queId);
            if (serviceQueId.size() == 0){
                data = 1;
            }
            else {
                Integer integer = collectService.delCollect(usId, "多选题", queId);
                data = integer;
            }
        }
        else if (queType == 3){
            List<Collect> serviceQueId = collectService.findQueId(usId, "单选题", queId);
            if (serviceQueId.size() == 0){
                data = 1;
            }
            else {
                Integer integer = collectService.delCollect(usId, "填空题", queId);
                data = integer;
            }
        }
        else if (queType == 4){
            List<Collect> serviceQueId = collectService.findQueId(usId, "单选题", queId);
            if (serviceQueId.size() == 0){
                data = 1;
            }
            else {
                Integer integer = collectService.delCollect(usId, "编程题", queId);
                data = integer;
            }
        }
        return data;
    }


}
